﻿var Class = {
    create: function () {
        return function () { this.initialize.apply(this, arguments); }
    }
}
var YJChart = Class.create();
YJChart.prototype = {
    initialize: function (option) {
        this.HasData = true;
        this.Table = document.getElementById(option.TableID);
        if (!!this.Table == false) {
            this.HasData = false;
            return;
        }
        this.ColStartIndex = (typeof option.ColStartIndex != "undefined" ? option.ColStartIndex : 0);
        this.ColEndIndex = typeof option.ColEndIndex != "undefined" ? option.ColEndIndex : 0; //负数表示倒数第几列
        this.RowStartIndex = typeof option.RowStartIndex != "undefined" ? option.RowStartIndex : 1;
        this.RowEndIndex = typeof option.RowEndIndex != "undefined" ? option.RowEndIndex : 0; //负数表示倒数第几行
        this.RowUnit = typeof option.RowUnit != "undefined" ? option.RowUnit : "";
        this.ColUnit = typeof option.ColUnit != "undefined" ? option.ColUnit : "";
        this.Title = typeof option.Title != "undefined" ? option.Title : "";
        this.Scale = typeof option.Scale != "undefined" ? option.Scale : 1;
        this.MaxLength = typeof option.MaxLength != "undefined" ? optionM.axLength : 5; //文本最大长度 -1表示不截取，默认为5，
        this.SetDefault();
    },
    SetDefault: function () {
        if (this.ColEndIndex <= 0) {
            this.ColEndIndex = this.Table.getElementsByTagName("tr")[0].getElementsByTagName("th").length - 1 + this.ColEndIndex; //GridView第一行是th
        }
        if (this.ColStartIndex <= 0) {
            this.ColStartIndex = this.Table.getElementsByTagName("tr")[0].getElementsByTagName("th").length - 1 + this.ColStartIndex; //GridView第一行是th
        }
        if (this.RowEndIndex < 0) {
            this.RowEndIndex = this.Table.getElementsByTagName("tr").length - 1 + this.RowEndIndex;
        }
        if (this.RowStartIndex < 0) {
            this.RowStartIndex = this.Table.getElementsByTagName("tr").length - 1 + this.RowStartIndex;
        }
    },
    //创建汇总
    ReadData_XY: function () {
        if (!this.HasData) { return ""; }
        var rows = this.Table.getElementsByTagName("tr");
        var str = '<graph title="' + this.Title + '" column_unit="' + this.ColUnit + '">';
        str += '<row >';
        if (rows.length == 3) {
            this.RowEndIndex = this.RowEndIndex + 1;
        }
        for (var i = this.RowStartIndex + 1; i <= this.RowEndIndex; i++) {
            str += '<column title="' + this.GetText($(rows[i]).find("th,td").get(this.ColStartIndex)) + '" info="">';
            var value = parseFloat(this.GetText($(rows[i]).find("th,td").get(this.ColEndIndex + 1))) / this.Scale;
            str += '<data unit="' + this.RowUnit + '" title="" value="' + value + '" alt="' + value + '" info=""></data>';
            str += '</column>';
        }
        str += '</row>';
        str += '</graph>';
        return str;
    },
    ReadData_YX: function () {
        if (!this.HasData) { return ""; }
        var rows = this.Table.getElementsByTagName("tr");
        var str = '<graph title="' + this.Title + '" column_unit="' + this.ColUnit + '">';
        str += '<row >';
        for (var i = this.ColStartIndex + 1; i <= this.ColEndIndex; i++) {
            str += '<column title="' + this.GetText($(rows[0]).find("th,td").get(i)) + '" info="">';
            var value = parseFloat(this.GetText($(rows[this.RowEndIndex + 1]).find("th,td").get(i))) / this.Scale;
            str += '<data unit="' + this.RowUnit + '" title="" value="' + value + '" alt="' + value + '" info=""></data>';
            str += '</column>';
        } str += '</row>';
        str += '</graph>';
        return str;
    },

    //第一行作为横坐标
    ReadDataXY: function () {
        //alert(this.ColStartIndex + 1);
        if (!this.HasData) { return ""; }
        var rows = this.Table.getElementsByTagName("tr");
        var str = '<graph title="' + this.Title + '" column_unit="' + this.ColUnit + '">';
        for (var i = this.RowStartIndex; i <= this.RowEndIndex; i++) {
            str += '<row title="' + this.GetText($(rows[i]).find("th,td").get(this.ColStartIndex)) + '" info="' + i + '">';

            for (var j = this.ColStartIndex + 1; j <= this.ColEndIndex; j++) {
                str += '<column title="' + this.GetText($(rows[0]).find("th,td").get(j)) + '" info="' + j + '">';
                var value = parseFloat(this.GetText($(rows[i]).find("th,td").get(j))) / this.Scale;
                str += '<data unit="' + this.RowUnit + '" title="" value="' + value + '" alt="' + value + '" info=""></data>';
                str += '</column>';
            }
            str += '</row>';
        }
        str += '</graph>';
        return str;
    },
    //第一列作为横坐标
    ReadDataYX: function () {
        if (!this.HasData) { return ""; }
        var rows = this.Table.getElementsByTagName("tr");
        var str = '<graph title="' + this.Title + '" column_unit="' + this.ColUnit + '">';
        for (var i = this.ColStartIndex + 1; i <= this.ColEndIndex; i++) {
            str += '<row title="' + this.GetText($(rows[0]).find("th,td").get(i)) + '" info="' + i + '">';
            for (var j = this.RowStartIndex; j <= this.RowEndIndex; j++) {
                str += '<column title="' + this.GetText($(rows[j]).find("th,td").get(this.ColStartIndex)) + '" info="' + j + '">';
                var value = parseFloat(this.GetText($(rows[j]).find("th,td").get(i))) / this.Scale;
                str += '<data unit="' + this.RowUnit + '" title="" value="' + value + '" alt="' + value + '" info=""></data>';
                str += '</column>';
            }
            str += '</row>';
        }
        str += '</graph>';
        return str;
    },
    //读取单列数据
    ReadColDataX: function (colIndex) {
        var rows = this.Table.getElementsByTagName("tr");
        var title = this.GetText($(rows[0]).find("th,td").get(colIndex));
        var str = '<graph title="' + this.Title + '" column_unit="' + this.ColUnit + '">';
        for (var i = this.RowStartIndex; i <= this.RowEndIndex; i++) {
            str += '<row title="' + this.CutText(this.GetText($(rows[i]).find("th,td").get(this.ColStartIndex))) + '" info="' + colIndex + '">'; //此处截取了字符串
            str += '<column title="' + title + '" info="' + i + '">';
            var value = parseFloat(this.GetText($(rows[i]).find("th,td").get(colIndex))) / this.Scale;
            str += '<data unit="' + this.RowUnit + '" title="" value="' + value + '" alt="' + value + '" info=""></data>';
            str += '</column>';
            str += '</row>';
        }
        str += '</graph>';
        return str;
    },
    //读取单行数据
    ReadRowDataX: function (RowIndex) {
        var row0 = this.Table.getElementsByTagName("tr")[0];
        var row = this.Table.getElementsByTagName("tr")[RowIndex];
        var title = this.GetText($(rows).find("th,td").get(this.ColStartIndex));
        var str = '<graph title="' + this.Title + '" column_unit="' + this.ColUnit + '">';
        for (var j = this.ColStartIndex + 1; j <= this.ColEndIndex; j++) {
            str += '<row title="' + this.CutText(this.GetText($(row0).find("th,td").get(i))) + '" info="' + RowIndex + '">'; //此处截取了字符串
            str += '<column title="' + title + '" info="' + j + '">';
            var value = parseFloat(this.GetText($(rows).find("th,td").get(j))) / this.Scale;
            str += '<data unit="' + this.RowUnit + '" title="" value="' + value + '" alt="' + value + '" info=""></data>';
            str += '</column>';
            str += '</row>';
        }
        str += '</graph>';
        return str;
    },
    //获取节点文本
    GetText: function (obj) {
        var t = "";
        obj = obj.childNodes || obj;
        for (var j = 0; j < obj.length; j++) {
            t += obj[j].nodeType != 1 ? obj[j].nodeValue : this.GetText(obj[j].childNodes);
        }
        return t;
    },
    //剪切字符串
    CutText: function (txt) {
        if (this.MaxLength != -1 && txt.length > this.MaxLength) {
            return txt.substr(0, this.MaxLength) + "…";
        }
        return txt;
    }
}

